import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.sql.rowset.*;
import com.sun.rowset.*;

import javax.swing.JComboBox;
import javax.swing.JButton;


public class LoginDialog extends JDialog {
	private JTextField txtUserID;
	private final JLabel lblUserID = new JLabel("UserID");
	private final JTextField txtPassword = new JTextField();

	String dlgstrUserID;
	String dlgstrPassword;
	int dlgiServer;
	AccessDBInterface accClientLogin;
	int iResult;
	private JComboBox cmbServer;
	private JLabel lblPassword;
	private JButton btnOK;
	private JButton btnClose;
	private JLabel lblServer;
	private JLabel lblLogIn;

	public String getdlgstrUserID() {
		return dlgstrUserID;
	}

	public void setdlgstrUserID(String dlgstrUserID) {
		this.dlgstrUserID = dlgstrUserID;
	}

	public String getDlgstrPassword() {
		return dlgstrPassword;
	}

	public void setDlgstrPassword(String dlgstrPassword) {
		this.dlgstrPassword = dlgstrPassword;
	}

	public int getDlgiServer() {
		return dlgiServer;
	}

	public void setDlgiServer(int dlgiServer) {
		this.dlgiServer = dlgiServer;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginDialog dialog = new LoginDialog(null, null, false, 0);
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the dialog.
	 */
	public LoginDialog(final ClientWindow cliwin, JFrame mainframe, boolean modal, int numberofservers) {
		
		
		super(mainframe, modal);
		
		dlgstrUserID = new String();
		dlgstrPassword = new String();
		dlgiServer = -1;

		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(null);
		
		lblLogIn = new JLabel("LOG IN");
		lblLogIn.setBounds(0, 11, 432, 38);
		lblLogIn.setFont(new Font("Tahoma", Font.BOLD, 30));
		lblLogIn.setHorizontalAlignment(SwingConstants.CENTER);
		getContentPane().add(lblLogIn);
		lblUserID.setBounds(37, 76, 57, 32);
		getContentPane().add(lblUserID);
		
		txtUserID = new JTextField();
		txtUserID.setBounds(118, 76, 252, 32);
		getContentPane().add(txtUserID);
		txtUserID.setColumns(10);
		
		lblPassword = new JLabel("Password");
		lblPassword.setBounds(37, 127, 66, 38);
		getContentPane().add(lblPassword);
		txtPassword.setBounds(118, 130, 252, 32);
		getContentPane().add(txtPassword);
		txtPassword.setColumns(10);
		
		lblServer = new JLabel("Server");
		lblServer.setBounds(37, 194, 46, 14);
		getContentPane().add(lblServer);
		
		Vector<String> vecString = new Vector<>(numberofservers);
		for (int i = 0; i < numberofservers; i++) {
			vecString.add("Server " + (i + 1));
		}
		cmbServer = new JComboBox(vecString);
		cmbServer.setBounds(118, 188, 252, 20);
		getContentPane().add(cmbServer);
		
		btnOK = new JButton("OK");
		btnOK.setBounds(70, 228, 89, 23);
		getContentPane().add(btnOK);
		
		btnClose = new JButton("Close");
		btnClose.setBounds(263, 228, 89, 23);
		getContentPane().add(btnClose);
		
		btnOK.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dlgstrUserID = txtUserID.getText();
				dlgstrPassword = txtPassword.getText();
				dlgiServer = cmbServer.getSelectedIndex();
				setVisible(false);
				
				if (dlgstrPassword.equals("") || dlgstrUserID.equals(""))
					JOptionPane.showMessageDialog(null, "You cannot leave any blank");
				else {
					try {
						cliwin.realLogin();
					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});

		btnClose.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				setVisible(false);
			}
		});
	}
	
	public JComboBox getCmbServer() {
		return cmbServer;
	}
	public JLabel getLblPassword() {
		return lblPassword;
	}
	public JButton getBtnOK() {
		return btnOK;
	}
	public JButton getBtnClose() {
		return btnClose;
	}
	public JLabel getLblServer() {
		return lblServer;
	}
	public JTextField gettxtUserID() {
		return txtUserID;
	}
	public JLabel getLblUsername() {
		return lblUserID;
	}
	public JLabel getLblLogIn() {
		return lblLogIn;
	}
	public JTextField getTxtPassword() {
		return txtPassword;
	}
}
